package threading;

import java.util.PriorityQueue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @author 徐志荣
 * @date 2023/3/21 19:53
 */
class MyTask implements  Comparable<MyTask>{
    public Runnable runnable;
    //使用绝对的时间戳
    public long time;
    public MyTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        //取当前的时间戳 + delay 作为该任务实际指定的时间戳
        this.time = System.currentTimeMillis() + delay;
    }
    @Override
    public int compareTo(MyTask o) {
        //这样的写法每次 : 每次取出的是时间最小的元素
        return (int)(time - o.time);
    }
}
class MyTimer{
    //这个结构,带有优先级的阻塞队列,核心数据结构
    private PriorityBlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();

    //创建一个锁对象
    private  Object locker = new Object();

    //此处的delay 是一个形如 3000 这样的数字 (表示的是,多长时间之后,执行该任务~)
    public void schedule(Runnable runnable,long delay){
        //根据参数,构造MyTask 插入队列即可
        MyTask myTask = new MyTask(runnable,delay);
        queue.put(myTask);
        synchronized (locker) {
            locker.notify();
        }
    }
    //创建线程,负责执行具体任务
    //构造方法~
    public MyTimer(){
        Thread t = new Thread(() ->{
            while (true){
                try {
                    //没有peek操作~
                    synchronized (locker) {
                        MyTask myTask = queue.take();
                        //1.没有元素 ->阻塞
                        //2. 有元素 ->往下执行~
                        //    获取当前时间
                        long curTime = System.currentTimeMillis();
                        if(myTask.time <= curTime){
                            //时间到了,可以执行任务了
                            myTask.runnable.run();
                        }else {
                            //时间还没到
                            //把刚才取出的任务,重新塞回到队列中
                            queue.put(myTask);
                            locker.wait(myTask.time - curTime);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }

}
public class ThreadDemo23 {
    public static void main(String[] args) {
       MyTimer myTimer = new MyTimer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello5");
            }
        },5000);
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello4");
            }
        },4000);
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello3");
            }
        },3000);
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello2");
            }
        },2000);
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello1");
            }
        },1000);
        System.out.println("hello0");
    }
}
